package com.github.data_manage.config

import cn.dev33.satoken.`fun`.SaFunction
import cn.dev33.satoken.interceptor.SaInterceptor
import cn.dev33.satoken.router.SaRouter
import cn.dev33.satoken.stp.StpUtil
import org.springframework.context.annotation.Configuration
import org.springframework.http.CacheControl
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer


/**
 * @author 18066
 * @since 2020/10/28
 */
@Configuration
open class WebConfig : WebMvcConfigurer {


    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
            .allowedHeaders("*")
            .allowedOriginPatterns("*")
            .allowedMethods("*")
            .allowCredentials(true)
            .maxAge(3600)
    }


    //    open override fun addViewControllers(registry: ViewControllerRegistry) {
//        registry.addRedirectViewController("/", "/dataManage/index.html")
//    }
//
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/**")
            .setCacheControl(CacheControl.noCache())
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")
            .setCacheControl(CacheControl.noCache())
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/")
            .setCacheControl(CacheControl.noCache())
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars**").addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCacheControl(CacheControl.noCache())

        super.addResourceHandlers(registry);
    }


    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(SaInterceptor {
            SaRouter
                .match("/**")
                .notMatch(
                    "/dataManage/auth/login",
                    "/dataManage/swagger-ui/**",
                    "/dataManage/swagger-ui**",
                    "/dataManage/v3/**",
                    "/dataManage/fundImportStatus/**",
                    "/dataManage/fundImportStatus",
                    "/auth/login",
                    "/swagger-ui/**",
                    "/swagger-ui**",
                    "/v3/**",
                    "/v2/**",
                    "/fundImportStatus/**",
                    "/fundImportStatus",
                    "/tests/**",
                    "/email/contact",
                    "/ai/**"
                )
                .check(SaFunction {
                    StpUtil.checkLogin()
                })
        }).addPathPatterns("/**")
            .excludePathPatterns(
                "/dataManage/auth/login",
                "/dataManage/swagger-ui/**",
                "/dataManage/swagger-ui**",
                "/dataManage/v3/**",
                "/dataManage/auth/login",
                "/dataManage/swagger-ui/**",
                "/dataManage/swagger-ui**",
                "/dataManage/v3/**",
                "/dataManage/fundImportStatus/**",
                "/dataManage/fundImportStatus",
                "/auth/login",
                "/swagger-ui/**",
                "/swagger-ui**",
                "/v3/**",
                "/auth/login",
                "/swagger-ui/**",
                "/swagger-ui**",
                "/v3/**",
                "/fundImportStatus/**",
                "/fundImportStatus",
                "/tests/**",
                "/email/contact",
                "/ai/**"
            )
    }

}
